package com.app.service;

import com.app.util.PropertiesHelper;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Murilo Dourado
 * Date: 12/2/11
 * Time: 7:42 PM
 */
public class ScreenShootService {

    /**
     * This method is responsible to take screen shoots.
     * @param type
     */
    public void getScreenShoot(String type){
        try{
            Long waitingTime = Long.parseLong(PropertiesHelper.getProperties("time.waiting"));
            System.out.println("Waiting time for next screen shoot " + (waitingTime / 1000L) + " seconds");
            int counterName = 0;
            Boolean fn = (type == "Play")? true : false;
            while(fn){

                Toolkit toolkit = Toolkit.getDefaultToolkit();
                //This will work for several types of resolution
                Dimension screenSize = toolkit.getScreenSize();
                Robot rb = new Robot();
                BufferedImage bi = rb.createScreenCapture(new Rectangle(screenSize));

                counterName++;
                String fileName = "screen_shoot"+counterName;
                // Saving the file image
                ImageIO.write(bi, "jpg", new File(fileName + ".jpg"));
                System.out.println("Screen shoot "+fileName+ " saved");
                try {
                    Thread.sleep(waitingTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
